import {GET_LIST_DATA,LEFT_CLICK,ADD_SELECT_ITEM,MINUS_SELECT_ITEM,SHOW_CHOOSE_CONTENT,CLEAR_CAR} from "../actions/actionTypes.js";
const initState = {
  listData:{},
  currentLeftIndex:0,
  showChooseContent:false,
  infoName:{}
}
const itemClick = (state,action)=>{
  return {...state,currentLeftIndex: action.obj.currentLeftIndex}
}
const getListData = (state,action)=>{
  if (Object.keys(state.listData).length>0){
    return {...state}
  }
  return {...state,listData: action.obj.data,infoName:action.obj.data}
}
const addSelectItem=(state,action)=>{
  let  listData =state.listData
  let list = listData.food_spu_tags || []
  let currentItem = list[action.index|| state.currentLeftIndex]
  currentItem.spus[action.obj.index].chooseCount ++
  let _listData = JSON.parse(JSON.stringify(listData))
  return {...state,listData: _listData}
}

const minusSelectItem=(state,action)=>{
  let  listData =state.listData
  let list = listData.food_spu_tags || []
  let currentItem = list[action.index||state.currentLeftIndex]
  currentItem.spus[action.obj.index].chooseCount --
  let _listData = JSON.parse(JSON.stringify(listData))
  return {...state,listData: _listData}
}
const showChooseContent = (state,action)=>{
  return {...state,showChooseContent: action.obj.flag}
}
const clearCar= (state)=>{
  let  listData =state.listData
  let list = listData.food_spu_tags || []
  for (let i = 0;i<list.length;i++){
    let spus = list[i].spus || []
    for (let j = 0; j< spus.length;j++){
      spus[j].chooseCount = 0;
    }
  }
  return{...state,listData: JSON.parse(JSON.stringify(listData))}
}
const menuReducer = (state = initState, action) => {
  switch (action.type) {
    case GET_LIST_DATA:
      return getListData(state,action)
    case LEFT_CLICK:
      return itemClick(state,action)
    case ADD_SELECT_ITEM:
      return addSelectItem(state,action)
    case MINUS_SELECT_ITEM:
      return minusSelectItem(state,action)
    case SHOW_CHOOSE_CONTENT:
      return showChooseContent(state,action)
    case CLEAR_CAR:
      return clearCar(state)
    default :
      return state;
  }
}
export default menuReducer